package BasicKnowledge.Basic;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @time: 2021/2/1/001 16:37
 * @author: Zhurunhe
 */
public class PropertiesDemo {
    public static void main(String[] args) throws IOException {
        Properties properties=new Properties();
        String path="com/zhurunhe/resources/config1.properties";
        //加载配置文件
        InputStream inputStream= PropertiesDemo.class.getResourceAsStream("/"+path);
        //多线程程序使用以下方法
        //InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
        properties.load(inputStream);//出现本行异常，出现空指针异常表明路径有误
        //读取配置信息
        System.out.println(properties.getProperty("name"));
        System.out.println(properties.get("pwd"));

        /**
         *
         * 另一种加载配置文件方式,不需要扩展名，但该文件的扩展名必须是properties
         */
        System.out.println("---------------------------------");
        ResourceBundle resourceBundle=ResourceBundle.getBundle("com/zhurunhe/resources/config1");
        System.out.println(resourceBundle.getString("name"));
        System.out.println(resourceBundle.getString("pwd"));

    }
}
